import c_screener_mode
import pygame

class cBasicSubmenu(c_screener_mode.c_screener_mode):
	def __init__(self):
		c_screener_mode.c_screener_mode.__init__(self)
		self.font = pygame.font.SysFont("Courier", 40)
		self.menu_title=""
		self.submenus_link=[]
		self.submenus_text=[]
		self.menuSurface=None
		self.pointer=0
		self.cursor=None
	def fill_via_file(self):
		source=open('locali\\'+self.locali+'\\'+self.file)
		text=source.readline().decode('utf-8').rstrip()
		self.menu_title_text=self.font.render(text, True, (0,1,190), (0, 0, 0))
		for i in range(0,len(self.submenus_link)):
			text=source.readline().decode('utf-8').rstrip()
			t0=self.font.render(text, True, (240,1,1), (0, 0, 0))
			t1=self.font.render(text, True, (1,150,1), (0, 0, 0))
			self.submenus_text.append((t0,t1))
		source.close()
	def includedSubmenus(self,menus):
		self.submenus_link=[]
		for m in menus:
			self.submenus_link.append(m)
	def makeMenu(self):
		self.menuSurface.fill((0,0,0))
		text=self.menu_title_text
		self.menuSurface.blit(text,(self.window_size[0]-text.get_width(),0))
		for i in xrange(0,len(self.submenus_text)):
			text=self.submenus_text[i][int(i==self.pointer)]
			self.menuSurface.blit(text,(70,i*70+70))
	def draw(self):
		#
		#self.gears.screen.fill((40,0,0))
		self.gears.screen.blit(self.getBackground(),(0,0))
		self.gears.screen.blit(self.menuSurface,(0,0))
		
		pygame.display.flip()
	def getBackground(self):
		bg=pygame.Surface(self.menuSurface.get_size())
		bg.fill((40,0,0))
		return bg
		#pass
	def do(self):
		g=pygame.event.get()
		remake=False
		for event in g:
			if event.type == pygame.KEYDOWN:
				if event.key == pygame.K_ESCAPE:
					self.switch_mode(None)
				if event.key == pygame.K_UP:
					self.setPointer(self.pointer-1)
					remake=True
				if event.key == pygame.K_DOWN:
					remake=True
					self.setPointer(self.pointer+1)
				if event.key == pygame.K_KP_ENTER or event.key == 13:
					self.switch_mode(self.submenus_link[self.pointer])
		if remake:
			self.makeMenu()
		self.draw()
	def locali_check(self):
		if self.locali!=self.gears.locali:
			self.locali=self.gears.locali
			self.fill_via_file()
	def setPointer(self,p):
		self.pointer=p%len(self.submenus_link)
	def configure_window(self):
		self.window_size=self.gears.screen_size
		print self.window_size
		print self.gears.screen.get_size()
		self.menuSurface=pygame.Surface(self.window_size)
		self.menuSurface.set_colorkey( (0, 0, 0) )
		self.makeMenu()
	def activate(self):
		self.setPointer(0)
		self.makeMenu()